'use strict';

import { cityinfoModel } from "../model/cityinfo";
import { ServiceBase, Exceptions } from '../kalend';

class CityInfoService extends ServiceBase {
    constructor() {
        super(cityinfoModel)
    }
    async query(type: any) {
        //console.log(type);
        return this.queryAll({
            filter: { status: 1},
            sort: { order: 1 }
        });
    }
    async save(doc: any) {
        if (doc._id) {
            this.update(doc._id, doc);
        } else {
            this.create(doc);
        }
        return '保存成功';
    }
    async querylist(params: any) {
        const offset = params.offset;
        const limit = params.limit;
        const type = params.type;
        const status = params.status;
        const title = params.title;
        const filter: any = {};
        if (title) {
            filter.$or = [{ title: { '$regex': title, $options: 'i' } }];
        }
        if (status === 0 || status === 1) {
            filter.status = status;
        }
        if (type && type != '') {
            filter.type = type;
        }
        let data = await this.queryPagination(offset, limit, {
            filter: filter,
            populateOptions: {
                path: 'createuser',
                select: 'name'
            }
        });
        data.rows = data.rows.map((item) => {
            item.createuser = item.createuser ? item.createuser.name : '';
            return item;
        })
        return data;
    }
}

export const cityinfoService = new CityInfoService();